// updated: 2014-01-12;

#pragma once

class Filer2 : public LObj
{
private:
	bool misstart;
	CTime mlasttime;
	CTime mtime;

	int mtimedif; // second;
	void setFirstStart( bool b ) { misstart = b; }

	CString mdir;
	CString mfnfirst;
	CString mfnmid;
	CString mfnlast;
	CString mfnlast2;
	CString mfnext;
	CString		mfn;

	CString getTimeStr( CTime & t);

	CString GetCurtimeStr( void );


	CStdioFile	mfile;
	bool mopmode;
	bool		misopen;

	unsigned int mdatlenmax;
	bool mislimit;

	int mlastcnt;
	void resetLastCount( ) { mlastcnt = 0;
		CString s;
		s.Format(TEXT("%d"),mlastcnt);
		if( mfnlast2.IsEmpty())
			mfnlast = s;
		else
			mfnlast = s+ TEXT("-") + mfnlast2;
	}
	void incLastCount() { mlastcnt++; 
		CString s;
		s.Format(TEXT("%d"),mlastcnt);
		if( mfnlast2.IsEmpty())
			mfnlast = s;
		else
			mfnlast = s + TEXT("-")+mfnlast2; 
	}

	uint mdatcnt;
	void incDatCount( int len ){ mdatcnt += len; }
	void resetDatCount(  ){ mdatcnt = 0; }

	//void setFilenameLast( CString fn ) { mfnlast = fn; }

protected:
	int init();
	void exit() { ; }
public:
	Filer2(void);
	~Filer2(void);

	void setLimit( bool b ) { mislimit = b; }
	void setDatLimit( unsigned int len ) { 
		if( len <=0 )	return;
		mdatlenmax = len; 
	}
	void setTimedif( int sec) { 
		if( sec <=0 ) return;
		mtimedif = sec; 
	}
	

	void reset();
	void setDir(CString dir ){ mdir = dir;}
	void setFilenameFirst( CString fn ) { mfnfirst = fn; }
	void setFilenameMid( CString fn ) { mfnmid = fn; }
	void setFilenameExt( CString fn ) { mfnext = fn; }
	void setFilenameLast(CString fn ) { mfnlast = fn;  mfnlast2=fn;}
	void updateFilename();
	CString getFilename() { return mfn; }
	void setOpenMode( int mode ) { mopmode = mode; }

	int openByTime();

	//int writeBin( unsigned char * buf, int len);



	int		OpenFileByTime( CString dir, CString tail, int mode );

	bool	isOpen() { return misopen; }
	int		OpenRead( CString fn, int mode = 0);
	int		Open( CString fn, int mode = 0 );
	void	Close( void );

	int		Write( unsigned char * buf, int len );
	int		WriteDb( double * buf, int len);
	void	writeLine(CString cs );

public:

};